<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\model;

use cores\exception\BaseException;
use app\common\model\RecommendContent as RecommendModel;
use app\store\model\RecommendImage as RecommendImageModel;
use app\store\model\RecommendGoods as RecommendGoodsModel;

/**
 * 推荐商品内容
 * Class Article
 * @package app\api\model
 */
class RecommendContent extends RecommendModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time'
    ];

      /**
     * 获取商品列表
     * @param array $param 查询条件
     * @param int $listRows 分页数量
     * @return mixed|\think\model\Collection|\think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [], int $listRows = 15)
    {
        // 整理查询参数
        $params = array_merge($param);
        
        if(isset($params['limit']) && $params['limit']>0){
            $listRows = (int)$params['limit'];
        }

        // 查询参数
        $params = $this->setQueryDefaultValue($param, [
            'title' => '',    // 文章标题
            'lock_status' => -1,    // 文章状态
        ]);

        // 检索查询条件
        $filter = [];
        // 文章标题
        !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
        // 文章状态
        $params['lock_status'] > -1 && $filter[] = ['lock_status', '=', $params['lock_status']];
    
        // 查询列表数据
        $list = $this->with(['image'])
            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($listRows);

        return $list;
    }


    
    /**
     * 获取文章详情
     * @param int $articleId 文章ID
     * @return static|null
     * @throws BaseException
     */
    public static function getDetail(int $recommendId)
    {
        // 获取文章详情
        $detail = parent::detail($recommendId, ['image']);
        if (empty($detail) || $detail['is_delete']) {
            throwError('很抱歉，当前文章不存在');
        }

        return $detail;
    }


}